<?php

class CitySelector extends CWidget {
	
	public $model;
	public $inputHtmlOptions = array();
	public $hiddenHtmlOptions = array();
	public $attribute;
	public $form;
	private static $_counter = 0;
	
	public function run() {
		
		$inputId = isset($this->inputHtmlOptions['id']) ? $this->inputHtmlOptions['id'] : 'CitySelector_'.++self::$_counter;
		$hiddenId = 'hide_'.$inputId;
		$this->id = $inputId;
		list($a, $b, $h) = CActiveFormAdv::resolveArgs($this->model, $this->attribute);
		$h = CUtilHelper::arr()->arrayStrMerge($this->inputHtmlOptions, $h, array('id'));
		$h['id'] = $inputId;
		$value = $a->$b;
		$textValue = '';
		if($value) {
			$city = CommonDistrict::model()->findByPk($value);
			$textValue = $city ? $city->name : '';
		}
		$h['value'] = $textValue;
		$input = $this->form->textField($this->model, $this->attribute, $h);
		
		$this->hiddenHtmlOptions['id'] = $hiddenId;
		$hidden = $this->form->hiddenField($this->model, $this->attribute, $this->hiddenHtmlOptions);
		$html = $this->template($this->id, $input, $hidden);
		$this->registerScript($inputId, $hiddenId);
		
		echo $html;
		
	}
	
	
	public function template($id, $input, $hidden) {
		
		
$template = <<<EOD
<span class='city-input-wrap' id='{$id}-wrap'>
	{$input}
	{$hidden}
	<span class='btn-city-input select-pop' inputId='{$this->id}'></span>
</span>
EOD;
		
		return $template;
	
	}
	
	public static function getHotCites() {
		return $cites = array(
			array('id'=>'1', 'value'=>'北京市'),
			array('id'=>'9', 'value'=>'上海市'),
			array('id'=>'289', 'value'=>'广州市'),
			array('id'=>'291', 'value'=>'深圳市'),
			array('id'=>'224', 'value'=>'青岛市'),
			array('id'=>'108', 'value'=>'大连市'),
			array('id'=>'175', 'value'=>'杭州市'),
			array('id'=>'162', 'value'=>'南京市'),
			array('id'=>'385', 'value'=>'成都市'),
			array('id'=>'258', 'value'=>'武汉市'),
			array('id'=>'22', 'value'=>'重庆市'),
			array('id'=>'325', 'value'=>'三亚市'),
			array('id'=>'204', 'value'=>'厦门市'),
			array('id'=>'438', 'value'=>'西安市'),
			array('id'=>'33', 'value'=>'香港'),
			array('id'=>'34', 'value'=>'澳门'),
			array('id'=>'107', 'value'=>'沈阳市'),
			array('id'=>'130', 'value'=>'哈尔滨市'),
			array('id'=>'275', 'value'=>'长沙市'),
			array('id'=>'406', 'value'=>'贵阳市'),
		);
		
		
	}
	
	
	public function registerScript($inputId, $hiddenId) {
		
		$url = $this->controller->createUrl("/cityAuto/getData");
		$checkUrl = $this->controller->createUrl('/cityAuto/getId');
		$cites = CJSON::encode($this->getHotCites());
		
		FrontDialog::registerAssetFiles('application.dialogSkins.yiibackend');
		$baseUrl = Yii::app()->baseUrl;
		$cs = Yii::app()->clientScript;
		$cs->registerScriptFile($baseUrl.'/scripts/autocomplete/jquery.autocomplete.pack.js');
		$cs->registerCssFile($baseUrl.'/scripts/autocomplete/jquery.autocomplete.css');
		$cs->registerScriptFile(Yii::app()->assetManager->publish(
			dirname(__FILE__).'/js/jquery.cityselector.js', false, -1, true
		));
		$cs->registerScript($inputId, "$('#{$inputId}').citySelector({id: '{$inputId}', cites:{$cites}, autoComplete:{requestUrl:'{$url}'}, 'checkUrl': '{$checkUrl}'})");
	}
	
	
}

?>